In [1]:
%matplotlib inline
import networkx as nx
import matplotlib.pyplot as plt

Introduction to graph theory


In [2]:
G = nx.Graph()
G.add_edge(1,2)
nx.draw_networkx(G)
plt.show()



In [3]:
G.add_nodes_from([3, 4])
nx.draw_networkx(G)
plt.show()



In [4]:
G.add_edge(3,4)
G.add_edges_from([(2, 3), (4, 1)])
nx.draw_networkx(G)
plt.show()



In [5]:
G.nodes()


Out[5]:
[1, 2, 3, 4]

In [6]:
G.edges()


Out[6]:
[(1, 2), (1, 4), (2, 3), (3, 4)]

In [7]:
G.adjacency_list()


Out[7]:
[[2, 4], [1, 3], [2, 4], [1, 3]]

In [8]:
nx.to_dict_of_lists(G)


Out[8]:
{1: [2, 4], 2: [1, 3], 3: [2, 4], 4: [1, 3]}

In [9]:
nx.to_edgelist(G)


Out[9]:
[(1, 2, {}), (1, 4, {}), (2, 3, {}), (3, 4, {})]

In [10]:
nx.to_numpy_matrix(G)


Out[10]:
matrix([[ 0.,  1.,  0.,  1.],
        [ 1.,  0.,  1.,  0.],
        [ 0.,  1.,  0.,  1.],
        [ 1.,  0.,  1.,  0.]])

In [11]:
print nx.to_scipy_sparse_matrix(G)


  (0, 1)	1
  (0, 3)	1
  (1, 0)	1
  (1, 2)	1
  (2, 1)	1
  (2, 3)	1
  (3, 0)	1
  (3, 2)	1

In [12]:
G.add_edge(1,3)
nx.draw_networkx(G)
plt.show()



In [13]:
G.degree()


Out[13]:
{1: 3, 2: 2, 3: 3, 4: 2}

In [14]:
plt.hist(nx.fast_gnp_random_graph(10000, 0.01).degree().values())


Out[14]:
(array([  1.50000000e+01,   1.96000000e+02,   1.01500000e+03,
          2.41900000e+03,   3.12000000e+03,   2.13800000e+03,
          8.78000000e+02,   1.96000000e+02,   2.10000000e+01,
          2.00000000e+00]),
 array([  65. ,   72.9,   80.8,   88.7,   96.6,  104.5,  112.4,  120.3,
         128.2,  136.1,  144. ]),
 <a list of 10 Patch objects>)

Graph algorithms


In [15]:
G = nx.krackhardt_kite_graph()
nx.draw_networkx(G)
plt.show()



In [16]:
print nx.has_path(G, source=1, target=9)
print nx.shortest_path(G, source=1, target=9)
print nx.shortest_path_length(G, source=1, target=9)


True
[1, 6, 7, 8, 9]
4

In [17]:
nx.betweenness_centrality(G)


Out[17]:
{0: 0.023148148148148143,
 1: 0.023148148148148143,
 2: 0.0,
 3: 0.10185185185185183,
 4: 0.0,
 5: 0.23148148148148148,
 6: 0.23148148148148148,
 7: 0.38888888888888884,
 8: 0.2222222222222222,
 9: 0.0}

In [18]:
nx.degree_centrality(G)


Out[18]:
{0: 0.4444444444444444,
 1: 0.4444444444444444,
 2: 0.3333333333333333,
 3: 0.6666666666666666,
 4: 0.3333333333333333,
 5: 0.5555555555555556,
 6: 0.5555555555555556,
 7: 0.3333333333333333,
 8: 0.2222222222222222,
 9: 0.1111111111111111}

In [19]:
nx.closeness_centrality(G)


Out[19]:
{0: 0.5294117647058824,
 1: 0.5294117647058824,
 2: 0.5,
 3: 0.6,
 4: 0.5,
 5: 0.6428571428571429,
 6: 0.6428571428571429,
 7: 0.6,
 8: 0.42857142857142855,
 9: 0.3103448275862069}

In [20]:
nx.eigenvector_centrality(G)


Out[20]:
{0: 0.35220918419838565,
 1: 0.35220918419838565,
 2: 0.28583482369644964,
 3: 0.481020669200118,
 4: 0.28583482369644964,
 5: 0.3976909028137205,
 6: 0.3976909028137205,
 7: 0.19586101425312444,
 8: 0.04807425308073236,
 9: 0.011163556091491361}

In [21]:
nx.clustering(G)


Out[21]:
{0: 0.6666666666666666,
 1: 0.6666666666666666,
 2: 1.0,
 3: 0.5333333333333333,
 4: 1.0,
 5: 0.5,
 6: 0.5,
 7: 0.3333333333333333,
 8: 0.0,
 9: 0.0}

In [22]:
import community # Community module for community detection and clustering

G = nx.powerlaw_cluster_graph(100, 1, .4)
partition = community.best_partition(G)

for i in set(partition.values()):
   print "Community", i
   members = list_nodes = [nodes for nodes in partition.keys() if partition[nodes] == i]
   print members

values = [partition.get(node) for node in G.nodes()]
nx.draw_spring(G, cmap = plt.get_cmap('jet'), node_color = values, node_size=30, with_labels=False)
plt.show()

print "Modularity score:", community.modularity(partition, G)


Community 0
[0, 6, 7, 12, 18, 23, 24, 35, 37, 40, 41, 42, 52, 59, 64, 72, 87, 88, 89, 90, 92, 96, 99]
Community 1
[1, 2, 4, 21, 27, 38, 44, 46, 51, 58, 63, 66, 78, 79, 80, 83, 91]
Community 2
[3, 10, 33, 55, 82, 85]
Community 3
[5, 11, 15, 22, 26, 31, 39, 48, 49, 53, 56, 60, 69, 77, 86, 93, 94, 97]
Community 4
[8, 14, 17, 30, 43, 57, 71, 81, 95, 98]
Community 5
[9, 25, 54, 76, 84]
Community 6
[13, 16, 28, 29, 45, 61, 65, 68, 70]
Community 7
[19, 32, 36, 62, 73]
Community 8
[20, 67, 74]
Community 9
[34, 47, 50, 75]
Modularity score: 0.756759514335

Graph loading, dumping, and sampling


In [23]:
dump_file_base = "dumped_graph"

# Be sure the dump_file file doesn't exist
def remove_file(filename):
    import os
    if os.path.exists(filename):
        os.remove(filename)

In [24]:
G = nx.krackhardt_kite_graph()

In [25]:
# GML format write and read
GML_file = dump_file_base + '.gml'
remove_file(GML_file)

nx.write_gml(G, GML_file)
G2 = nx.read_gml(GML_file)

assert(G.edges() == G2.edges())

In [26]:
# The same can be done with
# JSON, Adjacency List, Edge List, GEXF, GraphML and so on

In [27]:
import snowball_sampling
my_social_network = nx.Graph()
snowball_sampling.snowball_sampling(my_social_network, 2, 'alberto')
nx.draw(my_social_network)
plt.show()


Reching depth 0
 new nodes to investigate: ['alberto']
Reching depth 1
 new nodes to investigate: ['its_kerrie_duhh', 'ph8th', 'nightraven', 'melisssa', 'mischa', 'deifiedsoul', 'cookita', '______eric_', 'seraph76', 'msliebling', 'hermes3x3', 'eldebate', 'adriannevandal', 'clymore']

In [28]:
my_sampled_social_network = nx.Graph()
snowball_sampling.snowball_sampling(my_sampled_social_network, 3, 'alberto', sampling_rate=0.2)
nx.draw(my_sampled_social_network)
plt.show()


Reching depth 0
 new nodes to investigate: ['alberto']
Reching depth 1
 new nodes to investigate: ['its_kerrie_duhh', 'ph8th', 'nightraven', 'melisssa', 'mischa', 'deifiedsoul', 'cookita', '______eric_', 'seraph76', 'msliebling', 'hermes3x3', 'eldebate', 'adriannevandal', 'clymore']
Reching depth 2
 new nodes to investigate: ['daniel_981', 'pup_ajax', 'obscureterminus', 'inbredhatred', 'motleyprose', 'alphaschnitz', 'djbloodrose', 'puckotg22', 'charchar88', 'dawdle', 'decasia', 'charlie_bear', 'yukon72', 'socalmedic', 'averagecub', 'arshermetica', 'rwbadger', 'skate_0r_di3', 'wannacub', 'brye', 'downshifft', 'islandurboi206', 'iceraver', 'hockeyfag', 'uncutone666', 'popebuck1', 'brianflores', 'jeffla', 'comatt1', 'belinus', 'mikeboriqua', 'bibbles_is_pimp', 'needleboy', 'bogus825', 'ilu_qt3', 'infiniteblue', 'cincycub', 'bearpawly', 'stargirlms', 'littlecrazycub', 'heidilikesyou', 'prisonbitch', 'symphonicdreams', 'daydreamer80', 'barebackfucking', 'leolz', 'jameth', 'apollonmk', 'unleet', 'gordreece', 'ncgay75', 'madkevinp', '_lyke_whoa_hawt', 'txredneck', 'shoottothrill82', 'darkageofmatt', 'jimmychunga74', 'eeyoredung', 'nightcrawlerxp', 'rikig1969', 'enigmaboi78', 'latincub', 'mistahpat', 'paricon', 'likethecandybar', 'edduhduh', 'moonboynm', 'trickytoro', 'islandcubfree', 'badrobot68', 'mrbrightsider', 'drubear', 'drugs_love_cuts', 'tobeloved', 'leafshimmer', 'nessachan', 'chinadork', 'blktalon', 'prophecyboy', 'razzman', 'phoenixcub', 'bottlebrunette', 'poetmatt', 'wooferstl', 'ckluv76', 'lthrbear', 'leashdog', 'a_green_macaw', 'bearlover', 'pandas_papa', 'plus_guy', 'greekcub', 'hickory200', 'werecub', 'robdeluxe', 'tiffany_blonde_', 'directorleo', 'badfaggot', '_____jaymie', 'chironae', 'funnboi81', 'beachbum222', 'qtnsoca', 'hydro_messiah', 'testogve', 'jagbear', 'thegeekboi13', 'johnspiff', 'torcboy', 'flower899', 'sultmhoor', 'bbackboy80', 'water_bear', 'zephyrchuck', 'drazgoth', 'vianegativa', 'crisp_and_leafy', 'grimmdolly', 'tucsoncockboi', 'gamecockfan', 'hylandr', 'xxkillersloverx', 'gregorbehr', 'redteufel', 'fuzzface_63', 'da_clocks_tikin', 'technocowboy', 'jonobear', 'rhyno1975', 'moroccomole', 'uberdaddybear', 'penaranda', 'claddah76', 'rawsound', 'cupidboi79', 'this_is_sweet', 'deepthroatbear', 'bjtrent', 'mantoi_78', 'michelee___ox', 'handelwithcare', 'absenceofsun', 'nebulocity1976', 'salacious_pop', 'pastelpoetry', 'cubnurse', 'jeff69', 'tuluum', 'peterlicious', 'mysticstar5379', 'jman926', 'supercub', 'skullosvibe', 'necro_man', 'nverzeanu', 'soldierofsolace', 'productreed', 'quietgaysoldier', 'bearsbearsbears', 'notofthistime', 'pimpizzle', 'booboobob', 'pensivecub', 'djmrswhite', 'rimpup', 'martini_tim', 'b_a_l_l_e_r', 'zero_skateboard', 'njbearcub1', 'josh69x3', 'rei_saru', 'tr8ingty', 'morpheusnaptime', 'chris__', 'hidefbear', 'bacardibarbie', 'herbe', 'akira96', 'big_hoss', 'bri4u', 'sabin', 'maigremeg', 'freqflyer', 'izakthetwisted', 'paladincub21', 'libertycub', 'full_exposure', 'camiloj', 'grubbybastard', 'the_oc_is_love', 'scotty_2_shoes', 'scottchurch', 'popicn', 'flamechampion', '___billy__xo', 'havasu_headcase', 'polomex']